﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace CMTVision.Widgets.Impls
{
    public class TemplatePage:System.Web.UI.Page,CMTVision.Widgets.Common.ITemplatePage
    {
        #region Ctor

        public TemplatePage()
            :base()
        {
            IsInEditMode = false;
        }

        #endregion

        #region Implements ITemplatePage interface

        public int PageID
        {
            get
            {
                return ViewState["PageID"] == null ? 0 : Convert.ToInt32(ViewState["PageID"]);
            }
            set
            {
                ViewState["PageID"] = value;
            }
        }

        public bool IsInEditMode
        {
            get
            {
                return ViewState["IsInEditMode"] == null ? false : Convert.ToBoolean(ViewState["IsInEditMode"]);
            }
            set
            {
                ViewState["IsInEditMode"] = value;
            }
        }


        public System.Web.UI.Control TemplateContainer
        {
            get
            {
                return GetTemplateContainer();
            }
        }

        public virtual CMTVision.Widgets.Common.ITemplate LoadWidgetStoreTemplate()
        {
            if (PageID > 0)
            {
                var templatedataid = (from pageEle in XDocument.Load(Server.MapPath("~/Data/pagedata.xml")).Elements("pages").Elements("page")
                                      where pageEle.Element("id").Value == PageID.ToString()
                                      select pageEle.Element("templatedataid").Value).FirstOrDefault();
                if (templatedataid != null)
                {
                    var templateid = (from templatedata in XDocument.Load(Server.MapPath("~/Data/templatedata.xml")).Element("templatedatas").Elements("templatedata")
                                      where templatedata.Element("id").Value == templatedataid
                                      select templatedata.Element("templateid").Value).FirstOrDefault();
                    if (templateid != null)
                    {
                        var templateSrc = (from template in XDocument.Load(Server.MapPath("~/Data/templates.xml")).Element("templates").Elements("template")
                                           where template.Element("id").Value == templateid
                                           select template.Element("src").Value).FirstOrDefault();
                        if (templateSrc != null)
                        {
                            CMTVision.Widgets.Common.ITemplate template = Page.LoadControl(templateSrc) as CMTVision.Widgets.Common.ITemplate;
                            template.TemplateDataID = Convert.ToInt32(templatedataid);
                            (template as System.Web.UI.Control).ID = "Template" + templatedataid;
                            template.IsInEditMode = IsInEditMode;
                            template.LoadTemplateColumns(); 
                            return template;
                        }
                    }
                }
            }
            return null;
        }
        
        public virtual void ForceRefresh()
        {
            if (TemplateContainer != null)
            {
                CMTVision.Widgets.Common.ITemplate template = TemplateContainer.Controls[0] as CMTVision.Widgets.Common.ITemplate;
                if (template != null)
                {
                    template.IsInEditMode = IsInEditMode;
                    template.ForceRefresh();
                }
            }
        }

        #endregion

        #region Other Methods

        protected virtual System.Web.UI.Control GetTemplateContainer()
        {
            throw new InvalidOperationException("Need to override the method:'GetTemplateContainer'!");
        }

        #endregion
         
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected void InitializeWidgetsTemplate()
        {
            if (TemplateContainer != null)
            {
                TemplateContainer.Controls.Clear();
                //System.Web.UI.Control template = LoadWidgetStoreTemplate() as System.Web.UI.Control;
                //template.ID = Constants.TemplateID;
                //Template = template as CMTVision.Widgets.Common.ITemplate;
                TemplateContainer.Controls.Add(Template as System.Web.UI.Control);
            }
        }

         

        protected CMTVision.Widgets.Common.ITemplate template;
        public CMTVision.Widgets.Common.ITemplate Template
        {
            get
            {
                PageID = 1;
                IsInEditMode = true;
                if (template == null)
                    template = LoadWidgetStoreTemplate();
                return template;
            }
            set {
                template = value;
            }
        }
    }
}